"""
演示pool模块的用法
"""
import os
import random
import time
from multiprocessing import Pool


def run_task(name):
    """
    执行任务的方法
    :param name:
    :return:
    """
    print("Start run task.%s（%s）." % (name, os.getpid()))
    start = time.time()
    time.sleep(random.random() * 10)
    end = time.time()
    print("Task %s runs %0.2f seconds" % (name, end - start))


if __name__ == "__main__":
    print("Parent process is %s" % os.getpid())
    p = Pool(5)
    for i in range(6):
        p.apply_async(func=run_task, args=(i,))
    print('Waiting for all subprocesses done...')
    p.close()
    p.join()
    print('All subprocesses done.')
